|
|
|
|
|
|
|
On the other hand, if you are using the Data Access objects instead of the Data Control, you will need to add the error-handling routine to your project. As an example, if you are adding a new record to the database, you will need to trap for any error that might occur during the add process. |
|
|
|
|
|
|
|
|
Tip It is a good idea to open database tables in the Form_Load event. That way, if any errors occur, you can catch and fix them before any data access really occurs. |
|
|
|
|
|
|
|
|
|
The Error-Handling Process |
|
|
|
|
|
|
|
|
When you first start to think about creating an error-handling routine for your application, you should realize that error handlers in Visual Basic aren't as straightforward as they might have been in older, more procedure -driven languages such as COBOL or BASIC. There are several reasons why this is true. First, Visual Basic is event-driven, meaning that every event that occurs will trigger some operation or action. Second, Visual Basic uses a call stack to keep track of the routines being processed and to isolate local variables within a routine. When your application exits from a routine, it can lose track of the values of any internal variables it might have been using. This makes resuming execution after the error has been resolved very difficult. Finally, all errors are local. If an error occurs, it should be handled in the routine where it occurred, which means you must write an error handler for each routine that you have included in the application. |
|
|
|
|
|
|
|
|
Because of the way Visual Basic handles errors, you need to understand how Visual Basic searches application code for an active error routine. In addition, you need to know how to enable an error routine or, as Visual Basic calls it, an error trap. Before looking at the hierarchy of the error processing flow, lets take a look at the commands and methods that are available in Visual Basic to help you handle any errors that occur. There are two main Visual Basic commands that help you control the error process. These are the On Error and Resume statements. |
|
|
|
|
|
|
|
|
The On Error statement gives you the capability of enabling and disabling error traps within the application as well as specifying the location of the error-handling routine to execute for a given routine or form. The On Error statement comes in three different flavors: |
|
|
|
|
|